/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.service.search;

import com.david.documentation.domain.dao.search.SearchDao;
import com.david.documentation.domain.entities.AutocompleteData;
import com.david.documentation.domain.entities.doku.Doku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(value = "searchService")
@Transactional(rollbackFor = Exception.class)
public class SearchServiceImpl implements SearchService{

	@Autowired
	private SearchDao searchDao;

	@Transactional
	public List<Doku> findDokusByName(String searchTerms, Integer startIndex, Integer maxResults) {
		return searchDao.findDokusByName(searchTerms,startIndex,maxResults);
	}
	@Transactional
	public List<Doku> findDokusByNameAndDescription(String searchTerms, Integer startIndex, Integer maxResults) {
		return searchDao.findDokusByNameAndDescription(searchTerms,startIndex,maxResults);
	}
	@Transactional
	public List<String> findDokuNames(String searchTerms, Integer startIndex, Integer maxResults) {
		return searchDao.findDokuNames(searchTerms, startIndex, maxResults);
	}
	@Transactional
	public List<Doku> findDokuNamesSql(String searchTerms){
		return searchDao.findDokuNamesSql(searchTerms);
	}
	@Transactional
	public List<String> findDokuNamesStringSql(String searchTerms) {
		List<String> tmp = new ArrayList<String>();
		for (Doku doku : findDokuNamesSql(searchTerms)){
			tmp.add(doku.getName());
		}
		return tmp; 
	}

	public Doku findById(Long id) {
		return searchDao.findById(id);
	}

	public long size() {
		return searchDao.size();
	}

	public List<Doku> findAll() {
		return searchDao.findAll();
	}

	public List<Doku> findDokuNamesAndDescriptionSql(String searchTerms) {
		return searchDao.findDokuNamesAndDescriptionSql(searchTerms);
	}

	public List<Doku> findDokuNamesAndDescriptionSqlLimit(String searchterm, Integer startIndex, Integer maxResults) {
		return searchDao.findDokuNamesAndDescriptionSqlLimit(searchterm,startIndex,maxResults);
	}

	public List<Doku> findDokuNamesSqlLimit(String searchterm, Integer startIndex, Integer maxResults) {
		return searchDao.findDokuNamesSqlLimit(searchterm,startIndex,maxResults);
	}

	public List<AutocompleteData> findDokuNamesStringLimitSql(String searchTerms,Integer startIndex, Integer maxResults) {
		List<AutocompleteData> tmp = new ArrayList<AutocompleteData>();
		for (Doku doku : findDokuNamesSqlLimit(searchTerms,startIndex,maxResults)){
			tmp.add(new AutocompleteData(doku.getName(),""));
		}
		return tmp; 
	}

}
